package com.baway.park

import android.annotation.SuppressLint
import android.content.Intent
import android.os.CountDownTimer
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.baway.base.path.RouterPath
import com.baway.common.adapter.ImageBannerAdapter
import com.baway.common.entity.BannerEntity
import com.baway.common.view.BaseMvvmActivity
import com.baway.image.glide.GlideImgLoader
import com.baway.park.databinding.ActivitySplashBinding
import com.tencent.mmkv.MMKV
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.transformer.AlphaPageTransformer
import dagger.hilt.android.AndroidEntryPoint

/**
 * 作者：启动页面
 * 日期：2025/8/14
 * 描述：游戏鸿蒙-智慧园区-01-设备管理V1.1-20250115
 * 业务：1.倒计时；2.APP主图Banner切换；3.广告
 */

@AndroidEntryPoint
class SplashActivity : BaseMvvmActivity<ActivitySplashBinding>() {

    private var bannerData = mutableListOf<BannerEntity>()
    private lateinit var mBannerAdapter: ImageBannerAdapter
    private var mCountDownTimer: CountDownTimer? = null



    override fun getLayoutId(): Int = R.layout.activity_splash

    override fun initViews() {
        val isFisrt = MMKV.defaultMMKV().decodeBool("isFirstBoot", true)
        if (isFisrt){
            MMKV.defaultMMKV().encode("isFirstBoot", false)
            showAppBanner()
        }else{
            showAppAdv()
        }
    }

    /**
     * 展示APP主图
     */
    private fun showAppBanner(){
        //View.VISIBLE表示显示UI，View.INVISIBLE表示不显示UI，但是可以占据UI大小和位置，View.GONE表示既不显示UI也不占据大小和位置
        mDataBinding.clAppBanner.visibility = View.VISIBLE
        mDataBinding.clAppAdv.visibility = View.GONE

        bannerData.run {
            add(BannerEntity(0, com.baway.resource.R.drawable.wellcome_0, ""))
            add(BannerEntity(1, com.baway.resource.R.drawable.wellcome_1, ""))
            add(BannerEntity(2, com.baway.resource.R.drawable.wellcome_2, ""))
        }

        mBannerAdapter = ImageBannerAdapter(bannerData)

        mDataBinding.appBanner.run {
            setAdapter(mBannerAdapter)
            //关联生命周期
            addBannerLifecycleObserver(this@SplashActivity)
            //设置指示器
            indicator = CircleIndicator(this@SplashActivity)
            //设置切换效果
            addPageTransformer(AlphaPageTransformer())
            //设置页面切换监听
            addOnPageChangeListener(object: OnPageChangeListener{
                override fun onPageScrolled(position: Int, positionOffset: Float,positionOffsetPixels: Int) {}

                override fun onPageSelected(position: Int) {
                    //判断Banner是否滑动到最后一页
                    if (position == bannerData.size - 1){
                        mDataBinding.tvStart.visibility = View.VISIBLE
                    } else {
                        mDataBinding.tvStart.visibility = View.INVISIBLE
                    }
                }

                override fun onPageScrollStateChanged(state: Int) { }

            })
        }

        //点击立即进入
        mDataBinding.tvStart.setOnClickListener {
            this@SplashActivity.jumpMainPage()
            this@SplashActivity.finish()
        }
    }

    //展示广告
    private fun showAppAdv(){
        mDataBinding.clAppBanner.visibility = View.GONE
        mDataBinding.clAppAdv.visibility = View.VISIBLE

        //显示广告图片
        mDataBinding.ivAdv.run {
            GlideImgLoader.loadResImage(this, com.baway.resource.R.drawable.wellcome_1, this)
        }

        //5秒倒计时
        mCountDownTimer = object : CountDownTimer(5000, 1000){
            override fun onTick(millisUntilFinished: Long) {
                mDataBinding.tvAdvSkip.text = "${millisUntilFinished / 1000}秒跳过"
            }

            override fun onFinish() {
                this@SplashActivity.jumpMainPage()
            }
        }

        //启动倒计时
        mCountDownTimer?.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        mCountDownTimer?.cancel()
    }

    private fun jumpMainPage(){
        ARouter.getInstance().build(RouterPath.PATH_MAIN_PAGE).navigation()
        this@SplashActivity.finish()
    }




}